package com.xgr.wonderful.activity;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.easemob.EMCallBack;
import com.easemob.chat.EMChatManager;
import com.google.gson.Gson;
import com.markupartist.android.widget.ActionBar;
import com.markupartist.android.widget.ActionBar.Action;
import com.xgr.wonderful.MainActivity;
import com.xgr.wonderful.R;
import com.xgr.wonderful.activity.RegisterActivity;
import com.xgr.wonderful.activity.base.BaseActivity;
import com.xgr.wonderful.constant.Constant;
import com.xgr.wonderful.db.DatabaseUtil;
import com.xgr.wonderful.entity.User;
import com.xgr.wonderful.utils.HXUtils;
import com.xgr.wonderful.utils.HttpProxy;
import com.xgr.wonderful.utils.LocalLikeRelationsUtils;
import com.xgr.wonderful.utils.LogUtils;
import com.xgr.wonderful.utils.ToastFactory;
import com.xgr.wonderful.utils.HttpProxy.IHttpRequestListener;
import com.xgr.wonderful.utils.LocalUserUtils;
import com.xgr.wonderful.utils.SettingUtils;

public class WelcomeActivity extends BaseActivity implements OnClickListener {

	private Button registerBtn;
	private Button loginBtn;
	private ActionBar mActionBar ;
	private TextView mFindUser ;
	
	private HttpProxy httpProxy = null;

	@Override
	protected void onCreate(Bundle bundle) {
		// TODO Auto-generated method stub
		super.onCreate(bundle);
		setContentView(R.layout.activity_welcome);
		mActionBar = (ActionBar)findViewById(R.id.actionbar_welcome) ;
		registerBtn = (Button) findViewById(R.id.registerBtn);
		loginBtn = (Button) findViewById(R.id.loginBtn);
		mFindUser = (TextView)findViewById(R.id.welcome_finduser) ;
		
//		mActionBar.setTitle("微圈");
//		mActionBar.setDisplayHomeAsUpEnabled(false) ;
//		mActionBar.setHomeAction(new Action() {
//			@Override
//			public void performAction(View view) {
//				
//			}
//			
//			@Override
//			public int getDrawable() {
//				// TODO Auto-generated method stub
//				return R.drawable.weix_logo_blank;
//			}
//		}) ;
		
		registerBtn.setOnClickListener(this);
		loginBtn.setOnClickListener(this);
		mFindUser.setOnClickListener(this ) ;
		
		httpProxy = new HttpProxy() ;
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.registerBtn:
			redictToActivity(mContext, RegisterActivity.class, null);
			break;
		case R.id.loginBtn:
			//redictToActivity(mContext, MainActivity.class, null) ;
			loginWeiX() ;
			break;
		case R.id.welcome_finduser:
			redictToActivity(mContext, FindUserActivity.class, null) ;
			break ;
		default:
			break;
		}
	}
	
	private void loginWeiX (){
		String deviceid = SettingUtils.getDeviceId(mContext) ;
		LogUtils.i(TAG, "deviceid in welcome:"+ deviceid) ;
		LocalUserUtils.getInstance().init(mContext) ;//使用前一定要初始化
		//下面的情况 下，可以只登录EMchat，不再登录自己的
		//如果 是第一次安装，返回的是null,所以会走网上拉取的方法
		//LogUtils.i(TAG, "local user="+ String.valueOf(LocalUserUtils.getInstance().getLocalUser())) ;
		if (LocalUserUtils.getInstance().getLocalUser()!= null &&
				LocalUserUtils.getInstance().getLocalUser().getDeviceID().equals(deviceid)) {

			LocalLikeRelationsUtils.getInstance().buildLikeRelations(mContext);
			sputil.setLogin(true);

			String userid = String.valueOf(LocalUserUtils.getInstance()
					.getLocalUser().getID());
			showProgressDialog("正在登录中...", true) ;
			loginEM(userid, userid);
		}else {
			httpProxy.getUserByDeviceid(deviceid, new IHttpRequestListener() {
				
				@Override
				public void OnHttpRequestSuccessful(String result, int errCode) {
					if (errCode == Constant.ERR_HTTP_REQUEST_SUCCESS){
						User mUser = new Gson().fromJson(result, User.class) ;
						if (mUser != null) {
							LocalUserUtils.getInstance().saveLocalUser(
									mContext ,mUser ) ;
							LocalLikeRelationsUtils.getInstance().buildLikeRelations(mContext) ;
							sputil.setLogin(true) ;
							
							String userid = String.valueOf(
									LocalUserUtils.getInstance().getLocalUser().getID()) ;
							loginEM (userid , userid );
						}else {
							//说明 没有查询到用户，可能 是没有注册
							ToastFactory.getToast(mContext, result).show() ;
							//redictToActivity(mContext, RegisterActivity.class, null);
						}
						
					}else if (errCode == Constant.ERR_HTTP_REQUEST_FAILURE) {
						cancelProgressDialog() ;
						ToastFactory.getToast(mContext, result).show() ;
						LogUtils.i(TAG, "splash login fail") ;
					}
				}
				
				@Override
				public void OnHttpRequestStart() {
					showProgressDialog("正在登录中...", true) ;
				}
				
				@Override
				public void OnHttpRequestFailure(String info) {
					cancelProgressDialog() ;
					ToastFactory.getToast(mContext, "请查看您的网络").show() ;
					LogUtils.i(TAG, "ogin fail info:"+ info) ;
				}
			}) ;
		}
		
	}
	/**
	 * 确保 一定是登录的
	 * @param userid
	 * @param pwd
	 */
	protected void loginEM(String userid, String pwd ) {
		EMChatManager.getInstance().login(userid, pwd, new EMCallBack() {
			
			@Override
			public void onSuccess() {
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						cancelProgressDialog() ;
						//ToastFactory.getToast(mContext, "服务器登录成功").show() ;
						DatabaseUtil.getInstance().initConversationDao(mContext) ;
						HXUtils.EMLoadConversations();
						redictToActivity(mContext, MainActivity.class, null);
					}
				}) ;
			}
			
			@Override
			public void onProgress(int arg0, String arg1) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onError(int arg0, String arg1) {
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						cancelProgressDialog() ;
						ToastFactory.getToast(mContext, "请查看您的网络").show() ;
						//能到这样一步，说明 是已经注册过的
						redictToActivity(mContext, MainActivity.class, null) ;
					}
				}) ;
			}
		}) ;
		
	}

	
	
}
